import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { User } from '../../users/entities/user.entity';

/**
 * 支付方式
 */
export enum PaymentMethod {
  WECHAT = 'wechat',
  ALIPAY = 'alipay',
}

/**
 * 订单状态
 */
export enum OrderStatus {
  PENDING = 'pending',           // 待支付
  PAID = 'paid',                 // 已支付
  PROCESSING = 'processing',     // 处理中
  COMPLETED = 'completed',       // 已完成
  REFUNDING = 'refunding',       // 退款中
  REFUNDED = 'refunded',         // 已退款
  CANCELLED = 'cancelled',       // 已取消
  EXPIRED = 'expired'            // 已过期
}

/**
 * 支付状态（兼容旧代码）
 */
export enum PaymentStatus {
  PENDING = 'pending',
  PAID = 'paid',
  FAILED = 'failed',
  REFUNDED = 'refunded',
}

/**
 * 订单实体
 */
@Entity('orders')
export class Order {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  userId: number;

  @Column({ length: 50, unique: true })
  orderNo: string;

  @Column({ type: 'decimal', precision: 10, scale: 2 })
  amount: number;

  @Column({ length: 50 })
  subscriptionType: string;

  @Column({ comment: '订阅月数' })
  duration: number;

  @Column({ length: 100, nullable: true, comment: '套餐名称' })
  planName: string;

  @Column({ type: 'decimal', precision: 10, scale: 2, comment: '实际支付金额' })
  totalPrice: number;

  @Column({
    type: 'enum',
    enum: PaymentMethod,
    default: PaymentMethod.WECHAT,
  })
  paymentMethod: PaymentMethod;

  @Column({
    type: 'enum',
    enum: OrderStatus,
    default: OrderStatus.PENDING,
  })
  status: OrderStatus;

  @Column({
    type: 'enum',
    enum: PaymentStatus,
    default: PaymentStatus.PENDING,
  })
  paymentStatus: PaymentStatus;

  @Column({ length: 100, nullable: true })
  transactionId: string;

  @Column({ type: 'datetime', nullable: true })
  paidAt: Date;

  @Column({ type: 'datetime', nullable: true, comment: '过期时间' })
  expiredAt: Date;

  @CreateDateColumn()
  createdAt: Date;

  // 关联关系
  @ManyToOne(() => User, (user) => user.orders)
  @JoinColumn({ name: 'userId' })
  user: User;
}







